﻿@page "/map"

@using System.Text;
@using System.Text.Encodings.Web;
@using System.Text.Unicode;
@using System.ComponentModel.DataAnnotations;
@using System.Text.Json;
@inject IJSRuntime JsRuntime

<RadioGroup @bind-Value="coordinateType" Size="large" TValue="string"  OnChange="@OnSelectedCoordinateTypeChanged">
    <Radio RadioButton Value="@("WGS84")">WGS84</Radio>
    <Radio RadioButton Value="@("GCJ02")">GCJ02</Radio>
    <Radio RadioButton Value="@("BD09")">BD09</Radio>
</RadioGroup>
<br />
<AntDesign.Input @bind-Value="LngLat" style="width: 450px;" Size="large" AllowClear>
    <AddOnBefore>经纬度</AddOnBefore>
</AntDesign.Input>
<Button Type="primary" @onclick="Convert" Size="large">
    转换
</Button>
<Button Type="primary" @onclick="CreateMarker" Size="large">
    创建标注点
</Button>
<br />
<AntDesign.Input @bind-Value="WGS84_LngLat" style="width: 450px;" Size="large" readonly>
    <AddOnBefore>WGS84坐标系</AddOnBefore>
</AntDesign.Input>
<br />
<AntDesign.Input @bind-Value="GCJ02_LngLat" style="width: 450px;" Size="large" readonly>
    <AddOnBefore>GCJ02坐标系</AddOnBefore>
</AntDesign.Input>
<br />
<AntDesign.Input @bind-Value="BD09_LngLat" style="width: 450px;" Size="large" readonly>
    <AddOnBefore>BD09坐标系</AddOnBefore>
</AntDesign.Input>
<br />
@if (isOpen)
{
    <Alert Type="@AlertType.Error" Description="@ErrerMessage" Closable AfterClose="OnDismiss" />
    <br />
}

<div id="mapContainer" style="width: 1024px; height: 768px; "></div>

@code {

    string coordinateType = "GCJ02";

    private string LngLat= "113.87132,22.568962";

    private string WGS84_LngLat;

    private string GCJ02_LngLat;

    private string BD09_LngLat;

    private string ErrerMessage;

    private bool isOpen = false;

    protected override async void OnInitialized()
    {
        await JsRuntime.InvokeVoidAsync("mapInit");
    }

    void OnDismiss()
    {
        isOpen = !isOpen;
    }

    void OnSelectedCoordinateTypeChanged(string e)
    {
        if (string.IsNullOrEmpty(LngLat))
        {
            return;
        }
        string[] arr = LngLat.Split(new string[]{ ",", "，", "|", ":" }, StringSplitOptions.RemoveEmptyEntries);
        List<double> wgs84s = new List<double>();
        List<double> gcj02s = new List<double>();
        List<double> bd09s = new List<double>();
        for (var i=0;i< arr.Length; i=i+2)
        {
            if (double.TryParse(arr[i], out double lng) && double.TryParse(arr[i+1], out double lat))
            {
                if (e == "WGS84")
                {
                    wgs84s.Add(lng);
                    wgs84s.Add(lat);
                    var gcj02 = Coordtransform.Wgs84togcj02(lng, lat);
                    gcj02s.Add(gcj02[0]);
                    gcj02s.Add(gcj02[1]);
                    var bd09 = Coordtransform.Wgs84tobd09(lng, lat);
                    bd09s.Add(bd09[0]);
                    bd09s.Add(bd09[1]);
                }
                else if (e == "GCJ02")
                {
                    gcj02s.Add(lng);
                    gcj02s.Add(lat);
                    var wgs84 = Coordtransform.Gcj02towgs84(lng, lat);
                    wgs84s.Add(wgs84[0]);
                    wgs84s.Add(wgs84[1]);
                    var bd09 = Coordtransform.Gcj02tobd09(lng, lat);
                    bd09s.Add(bd09[0]);
                    bd09s.Add(bd09[1]);
                }
                else if (e == "BD09")
                {
                    bd09s.Add(lng);
                    bd09s.Add(lat);

                    var wgs84 = Coordtransform.Bd09towgs84(lng, lat);
                    wgs84s.Add(wgs84[0]);
                    wgs84s.Add(wgs84[1]);

                    var gcj02 = Coordtransform.Bd09togcj02(lng, lat);
                    gcj02s.Add(gcj02[0]);
                    gcj02s.Add(gcj02[1]);

                }
            }
        }
        WGS84_LngLat = string.Join(',', wgs84s);
        GCJ02_LngLat = string.Join(',', gcj02s);
        BD09_LngLat = string.Join(',', bd09s);
    }

    [JSInvokable]
    public void Convert()
    {
        OnSelectedCoordinateTypeChanged(coordinateType);
    }

    [JSInvokable]
    public async void CreateMarker()
    {
        if (string.IsNullOrEmpty(GCJ02_LngLat))
        {
            return;
        }
        string[] arr = GCJ02_LngLat.Split(',', StringSplitOptions.RemoveEmptyEntries);
        if (arr.Length == 2)
        {
            await JsRuntime.InvokeVoidAsync("createMarker", arr);
        }
        else if(arr.Length > 2)
        {
            List<List<string>> latlngs = new List<List<string>>();
            for (var i = 0; i < arr.Length; i = i + 2)
            {
                List<string> latlng = new List<string>();
                latlng.Add(arr[i]);
                latlng.Add(arr[i+1]);
                latlngs.Add(latlng);
            }
            await JsRuntime.InvokeVoidAsync("createMarkers", latlngs);
        }
    }
}
